<template>
  <el-dialog
    :model-value="dialogVisible"
    @update:modelValue="val => $emit('update:dialogVisible', val)"
    title="新增商品"
    width="600px"
  >
    <el-form :model="form" label-width="100px" ref="formRef">
      <el-form-item label="商品名称" prop="name">
        <el-input v-model="form.name" placeholder="请输入商品名称" />
      </el-form-item>
      
      <el-form-item label="商品描述" prop="description">
        <el-input
          v-model="form.description"
          type="textarea"
          :rows="3"
          placeholder="请输入商品描述"
        />
      </el-form-item>

      <el-form-item label="商品价格" prop="price">
        <el-input-number
          v-model="form.price"
          :precision="2"
          :min="0"
          controls-position="right"
        />
      </el-form-item>

      <el-form-item label="库存数量" prop="stock">
        <el-input-number v-model="form.stock" :min="0" />
      </el-form-item>

      <el-form-item label="商品分类" prop="category">
        <el-select v-model="form.category" placeholder="请选择分类">
          <el-option
            v-for="item in categories"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
    </el-form>

    <template #footer>
      <span class="dialog-footer">
        <el-button @click="$emit('update:dialogVisible', false)">取消</el-button>
        <el-button type="primary" @click="handleSubmit">确认添加</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script setup>
import { reactive, ref } from 'vue'
import { ElMessage } from 'element-plus'

const emit = defineEmits(['refresh', 'update:dialogVisible'])

const props = defineProps({
  dialogVisible: {
    type: Boolean,
    default: false
  }
})

const formRef = ref(null)
const form = reactive({
  name: '',
  description: '',
  price: 0,
  stock: 0,
  category: ''
})

const categories = [
  { value: 'fresh', label: '生鲜食品' },
  { value: 'drinks', label: '酒水饮料' },
  { value: 'snacks', label: '休闲零食' }
]

const handleSubmit = async () => {
  try {
    await formRef.value.validate()
    // 这里添加实际提交逻辑
    ElMessage.success('商品添加成功')
    emit('refresh')
    emit('update:dialogVisible', false)
  } catch (error) {
    ElMessage.warning('请完善表单信息')
  }
}
</script>